package view;

import java.awt.Color;
import java.awt.Component;
import java.awt.Dimension;

import javax.swing.CellRendererPane;
import javax.swing.ImageIcon;
import javax.swing.JLabel;
import javax.swing.JTable;
import javax.swing.WindowConstants;
import javax.swing.JFrame;
import javax.swing.table.AbstractTableModel;
import javax.swing.table.TableCellRenderer;

import observer.Observer;

import model.Environment;
import model.FlatComponents.Type;


/**
* This code was edited or generated using CloudGarden's Jigloo
* SWT/Swing GUI Builder, which is free for non-commercial
* use. If Jigloo is being used commercially (ie, by a corporation,
* company or business for any purpose whatever) then you
* should purchase a license for each developer using Jigloo.
* Please visit www.cloudgarden.com for details.
* Use of Jigloo implies acceptance of these licensing terms.
* A COMMERCIAL LICENSE HAS NOT BEEN PURCHASED FOR
* THIS MACHINE, SO JIGLOO OR THIS CODE CANNOT BE USED
* LEGALLY FOR ANY CORPORATE OR COMMERCIAL PURPOSE.
*/
/**
 * 
 * @author Rino
 *
 */
public class PanelFlat extends javax.swing.JPanel implements Observer {

	private JTable tableFlat;    	//Tabella utilizzata per il drowing del FLat
	private MyTableModel myModel;	//Modello utilizzato per la gestione della tabella
	private MyCellRenderer cellRenderer;
	private Environment localEnv;
	
	public PanelFlat(Environment mainEnv) {
		super();
		localEnv=mainEnv;
		localEnv.addObserver(this);
		myModel=new MyTableModel();
		cellRenderer=new MyCellRenderer();
		tableFlat=new JTable(myModel);
		initGUI();
		addFeatures();
	}
	
	public void addFeatures(){
		for(int i=0;i<tableFlat.getColumnCount();i++){
			tableFlat.getColumnModel().getColumn(i).setCellRenderer(cellRenderer);
			tableFlat.getColumnModel().getColumn(i).setWidth(100);
		}
		
		tableFlat.setRowHeight(55);
	}
	
	private void initGUI() {
		try {
			setPreferredSize(new Dimension(909, 600));
			
			//Aggiunta Tabella Flat
			{
				
			tableFlat.setPreferredSize(new Dimension(800,500));
			add(tableFlat);
			this.setBackground(new java.awt.Color(192,192,192));
			
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 * Aggiorna l'interfaccia del pannello FLat
	 */
	public void updateGUI(){
		tableFlat.updateUI();
		//myModel.fireTableDataChanged();
	}
	
	private class MyTableModel extends AbstractTableModel{

		@Override
		public int getColumnCount() {
			// TODO Auto-generated method stub
			return localEnv.flat.width;
		}

		@Override
		public int getRowCount() {
			// TODO Auto-generated method stub
			return localEnv.flat.length;
		}

		@Override
		public Object getValueAt(int row, int col) {
			// TODO Auto-generated method stub
			return localEnv.getFlat().get(row, col);
		}
		
	}

	private class MyCellRenderer implements TableCellRenderer{

		@Override
		public Component getTableCellRendererComponent(JTable table,
				Object value, boolean isSelected, boolean hasFocus, int row,
				int col) {
			// TODO Auto-generated method stub
			JLabel label=new JLabel();
	
			
			if(localEnv.getFlat().get(row,col).equals(Type.KITCHEN))
				label.setIcon(new ImageIcon( new ImageIcon("src/img/kitchen.jpg").getImage().getScaledInstance(100, 55, 30)));
			else if(localEnv.getFlat().get(row,col).equals(Type.BEDROOM))
				label.setIcon(new ImageIcon( new ImageIcon("src/img/bedroom.jpg").getImage().getScaledInstance(100, 55, 30)));
			else if(localEnv.getFlat().get(row,col).equals(Type.LIVINGROOM))
				label.setIcon(new ImageIcon( new ImageIcon("src/img/livingroom.jpg").getImage().getScaledInstance(100, 55, 30)));
			else if(localEnv.getFlat().get(row,col).equals(Type.HALL))
				label.setIcon(new ImageIcon( new ImageIcon("src/img/utilityroom.jpg").getImage().getScaledInstance(100, 55, 30)));
			else if(localEnv.getFlat().get(row,col).equals(Type.MEAL))
				label.setIcon(new ImageIcon( new ImageIcon("src/img/meal.jpg").getImage().getScaledInstance(100, 55, 30)));
			else if(localEnv.getFlat().get(row,col).equals(Type.SMOKING))
				label.setIcon(new ImageIcon( new ImageIcon("src/img/smoking.jpg").getImage().getScaledInstance(100, 55, 30)));
			else if(localEnv.getFlat().get(row,col).equals(Type.TABLE))
				label.setIcon(new ImageIcon( new ImageIcon("src/img/table.jpg").getImage().getScaledInstance(100, 55, 30)));
			else if(localEnv.getFlat().get(row,col).equals(Type.FLOWERS))
				label.setIcon(new ImageIcon( new ImageIcon("src/img/flowers.jpg").getImage().getScaledInstance(100, 55, 30)));
			else if(localEnv.getFlat().get(row,col).equals(Type.DOOR))
				label.setIcon(new ImageIcon( new ImageIcon("src/img/door.jpg").getImage().getScaledInstance(100, 55, 30)));
			else if(localEnv.getFlat().get(row,col).equals(Type.WALL))
				label.setIcon(new ImageIcon( new ImageIcon("src/img/wall.jpg").getImage().getScaledInstance(100, 55, 30)));
			
			 if(row==localEnv.agent.x && col==localEnv.agent.y)
				label.setIcon(new ImageIcon( new ImageIcon("src/img/ciccio.jpg").getImage().getScaledInstance(100, 55, 30)));

					
			return label;
		}
		
	}

	@Override
	public void update(Object object) {
		// TODO Auto-generated method stub
		updateGUI();
		
	}



}
